//
//  SetUpViewController.m
//  时尚男士
//
//  Created by Jupiter on 15/6/3.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "SetUpViewController.h"

@interface SetUpViewController ()
@property (nonatomic, assign)BOOL *switchOn;
@property (nonatomic, retain)UIView *myView;
@property (nonatomic, retain)UILabel *label;
@property (nonatomic, retain)UIView *myView1;
@property (nonatomic, retain)UIView *myView2;
@property (nonatomic, retain)UILabel *label1;
@property (nonatomic, retain)UIButton *mybutton;
@property (nonatomic, assign)float fileSize;
@end

@implementation SetUpViewController
- (void)dealloc{
    [_mybutton release];
    [_myView release];
    [_label release];
    [_myView1 release];
    [_myView2 release];
    [_label1 release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.jpg"]];
    self.navigationController.navigationBar.translucent = NO;
    // Do any additional setup after loading the view.
    self.myView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 60)];
    CALayer * layer = [self.myView layer];
    layer.borderColor = [[UIColor lightGrayColor] CGColor];
    layer.borderWidth = 0.5f;
    [self.view addSubview:self.myView];
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 60)];
    self.label.text =@"更换主题";
    [self.myView addSubview:self.label];
    self.myView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0 , self.view.frame.size.width - 120, 60)];
    [self.view addSubview:self.myView1];
    UISwitch *mySwithc = [[UISwitch alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 60, 10, 40, 40)];
    [self.myView addSubview:mySwithc];
    [mySwithc addTarget:self action:@selector(switchChange:) forControlEvents:UIControlEventValueChanged];
    [self.myView release];
    [self.myView1 release];
    
    self.myView2 = [[UIView alloc] initWithFrame:CGRectMake(0, 90, self.view.frame.size.width, 60)];
    CALayer * layer1 = [self.myView2 layer];
    layer1.borderColor = [[UIColor lightGrayColor] CGColor];
    layer1.borderWidth = 0.5f;
    [self.view addSubview:self.myView2];
    self.label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 60)];
    self.label1.text =@"清除缓存";
    [self.myView2 addSubview:self.label1];
    self.mybutton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 60, 10, 40, 40)];
    [self.mybutton setBackgroundImage:[UIImage imageNamed:@"1qingchu"] forState:UIControlStateNormal];
    [self.mybutton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.myView2 addSubview:self.mybutton];
    [_mybutton release];
    NSUserDefaults *USER = [NSUserDefaults standardUserDefaults];
    mySwithc.on = [USER boolForKey:@"name"];
    if (![USER boolForKey:@"name"]) {
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg1.jpg"]];
    }
    
}

- (long long)fileSizeAtPath:(NSString*)filePath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
}

- (float)folderSizeAtPath:(NSString*)folderPath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath]) return 0;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    NSString* fileName;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    //NSString *str =
    
    
    NSLog(@"=======================%f",folderSize/(1024.0*1024.0));
    return folderSize/(1024.0*1024.0);
}


- (void)buttonAction:(UIButton *)myButton{
    //彻底清除缓存第一种方法
        dispatch_async(
                   dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
                   , ^{
                       NSString *cachPath = [NSSearchPathForDirectoriesInDomains( NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
                       self.fileSize = [self folderSizeAtPath:cachPath];

                       
                       NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
                       NSLog(@"files :%d",[files count]);
                       for (NSString *p in files) {
                           NSError *error;
                           NSString *path = [cachPath stringByAppendingPathComponent:p];
                           if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                               [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                           }
                       }
                       [self performSelectorOnMainThread:@selector(clearCacheSuccess) withObject:nil waitUntilDone:YES];});
    NSLog(@"---");
}


-(void)clearCacheSuccess
{
    UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"清除成功" message:[NSString stringWithFormat:@"清除%f兆",self.fileSize/(1024.0*1024.0)] delegate:self cancelButtonTitle:@"确认" otherButtonTitles:@"取消", nil];
    [alter show];
    NSLog(@"清理成功");
}

- (void)switchChange:(UISwitch *)mySwitch{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    [user setBool:mySwitch.on forKey:@"name"];
    [user synchronize];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"name" object:nil];
    if (mySwitch.on) {
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.jpg"]];
    }else{
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg1.jpg"]];
    }
    
   }
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
